package com.czy.owner.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.api.GetCustomerApi;
import com.czy.owner.api.PushGoodsApi;
import com.czy.owner.api.UpdateOrderStatusApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.MessageContacts;
import com.czy.owner.entity.OrdersListModel;
import com.czy.owner.entity.PushGoodsModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.ui.hotshop.GoodsDetailActivity;
import com.czy.owner.ui.message.ChatActivity;
import com.czy.owner.ui.message.ContactsPanelActivity;
import com.czy.owner.ui.order.EvaluateActivity;
import com.czy.owner.ui.order.LogisticsActivity;
import com.czy.owner.ui.order.OrderActivity;
import com.czy.owner.ui.order.OrderDetailActivity;
import com.czy.owner.ui.order.OrderPaidActivity;
import com.czy.owner.ui.order.SeeEvaluateActivity;
import com.czy.owner.utils.GsonUtils;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.tiputils.MProgressDialog;
import com.czy.owner.widget.UniversalDialog;
import com.easemob.cases.MessageHelper;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.Serializable;

import static com.hyphenate.easeui.domain.EaseUser.USER_TYPE_SYSTEM;

/**
 * Created by wangyang on 2017/5/18 0018.
 */

public class OrderAdapter extends RecyclerArrayAdapter<OrdersListModel.ListBean> {
    // waitPay：待付款，waitDelivery：待发货，delivery：已发货，toInstall：待安装，completed：已完成，commented：已评价，cancelled：已取消
    private static final int WAITPAY = 1;
    private static final int WAITDELIVERY = 2;
    private static final int DELIVERY = 3;
    private static final int TOINSTALL = 4;
    private static final int COMPLETED = 5;
    private static final int COMMENTED = 6;
    private static final int CANCELLED = 7;
    private static final int DEFAULT = 0;
    private Context context;

    public OrderAdapter(Context context) {
        super(context);
        this.context = context;
    }


    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderViewHolder(parent);
    }

    class OrderViewHolder extends BaseViewHolder<OrdersListModel.ListBean> {
        private LinearLayout llOrder;
        private TextView tvStoreName;
        private TextView tvPayStatus;
        private EasyRecyclerView recyclerViewSub;
        private TextView tvRufuned;
        private TextView tvTotalPrice;
        private TextView tvBottomOrderRight;
        private TextView tvBottomOrderCenter;
        private TextView tvBottomOrderLeft;
        private OrderSubAdapter orderSubAdapter;
        private Double detailActualPayment = 0.00;

        public OrderViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_order);
            llOrder = $(R.id.ll_order);
            tvStoreName = $(R.id.tv_store_name);
            tvPayStatus = $(R.id.tv_pay_status);
            recyclerViewSub = $(R.id.recyclerView_sub);
            tvRufuned = $(R.id.tv_rufuned);
            tvTotalPrice = $(R.id.tv_total_price);
            tvBottomOrderRight = $(R.id.tv_bottom_order_right);
            tvBottomOrderCenter = $(R.id.tv_bottom_order_center);
            tvBottomOrderLeft = $(R.id.tv_bottom_order_left);
        }

        @Override
        public void setData(final OrdersListModel.ListBean data) {
            super.setData(data);
            if (data.getStoreId() != 0) {
                if (data.getFromGoodsId() != 0) {
                    tvStoreName.setText("平台转移");
                } else {
                    tvStoreName.setText(data.getStoreName());
                }
            } else {
                tvStoreName.setText("平台自营");
            }
            //waitPay：待付款，waitDelivery：待发货，delivery：已发货，toInstall：待安装，completed：已完成，commented：已评价，cancelled：已取消
            String orderStatus = data.getOrderStatus();
            if (orderStatus.equals("waitPay")) {
                tvPayStatus.setText("等待买家付款");
                tvBottomOrderRight.setVisibility(View.VISIBLE);
                tvBottomOrderRight.setText("付款");
                tvBottomOrderCenter.setText("取消订单");
                tvBottomOrderLeft.setVisibility(View.VISIBLE);

                if (data.getStoreId() != 0) {
                    if (data.getFromGoodsId() != 0) {
                        tvBottomOrderLeft.setText("联系客服");
                    } else {
                        tvBottomOrderLeft.setText("联系卖家");
                    }
                } else {
                    tvBottomOrderLeft.setText("联系客服");
                }

            } else if (orderStatus.equals("waitDelivery")) {
                tvPayStatus.setText("等待卖家发货");
                tvBottomOrderRight.setVisibility(View.GONE);
                if (data.getStoreId() != 0) {
                    if (data.getFromGoodsId() != 0) {
                        tvBottomOrderCenter.setText("联系客服");
                    } else {
                        tvBottomOrderCenter.setText("联系卖家");
                    }
                } else {
                    tvBottomOrderCenter.setText("联系客服");
                }
                tvBottomOrderLeft.setVisibility(View.GONE);
            } else if (orderStatus.equals("delivery")) {
                if (data.getStoreId() != 0) {
                    tvPayStatus.setText("卖家已发货");
                } else {
                    tvPayStatus.setText("平台已发货");
                }
                tvBottomOrderRight.setVisibility(View.VISIBLE);
                tvBottomOrderRight.setText("确认收货");
                if(data.getIsShippingToStore().equals("true")){
                    tvBottomOrderCenter.setVisibility(View.GONE);
                }else {
                    tvBottomOrderCenter.setVisibility(View.VISIBLE);
                }
                tvBottomOrderCenter.setText("查看物流");
                tvBottomOrderLeft.setVisibility(View.GONE);
            } else if (orderStatus.equals("toInstall")) {
                tvPayStatus.setText("买家已收货");
                tvBottomOrderRight.setVisibility(View.VISIBLE);
                tvBottomOrderRight.setText("确认安装");

                if(data.getIsShippingToStore().equals("true")){
                    tvBottomOrderCenter.setVisibility(View.GONE);
                }else {
                    tvBottomOrderCenter.setVisibility(View.VISIBLE);
                }
                tvBottomOrderCenter.setText("查看物流");
                tvBottomOrderLeft.setVisibility(View.GONE);
            } else if (orderStatus.equals("completed")) {
                tvPayStatus.setText("交易完成");
                tvBottomOrderRight.setVisibility(View.GONE);
                tvBottomOrderCenter.setText("去评价");
                tvBottomOrderLeft.setVisibility(View.GONE);
            } else if (orderStatus.equals("commented")) {
                tvPayStatus.setText("已评价");
                tvBottomOrderRight.setVisibility(View.GONE);
                tvBottomOrderCenter.setText("查看评价");
                tvBottomOrderLeft.setVisibility(View.GONE);
            } else if (orderStatus.equals("cancelled")) {
                tvPayStatus.setText("已取消");
                tvBottomOrderRight.setVisibility(View.GONE);
                tvBottomOrderCenter.setText("删除订单");
                tvBottomOrderLeft.setVisibility(View.GONE);
            } else {
                tvPayStatus.setText("");
            }

            recyclerViewSub.setLayoutManager(new LinearLayoutManager(context));
            recyclerViewSub.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
            recyclerViewSub.setAdapter(orderSubAdapter = new OrderSubAdapter(context));
            orderSubAdapter.addAll(data.getOrderItems());
            orderSubAdapter.setOnItemClickListener(new OnItemClickListener<OrdersListModel.ListBean.OrderItemsBean>() {
                @Override
                public void onItemClick(View view, int position, OrdersListModel.ListBean.OrderItemsBean subModel) {
                    if (PhoneUtils.isNetworkConnected(context)) {
                        Intent ti = new Intent(context, OrderDetailActivity.class);
                        ti.putExtra("orderId", subModel.getOrderId());
                        context.startActivity(ti);
                    }

                }
            });

            int count = 0;
            for (int i = 0; i < data.getOrderItems().size(); i++) {
                count += data.getOrderItems().get(i).getCount();
            }


            double discountPrice = 0.00;
            // 优惠金额
            if (data.getOrderItems().size() > 0) {
                for (int i = 0; i < data.getOrderItems().size(); i++) {
                    discountPrice += data.getOrderItems().get(i).getDiscountPrice();
                }

            }

            // 优惠金额
//            double denomination = data.getDenomination();

            //安装费
            double servicePrice = 0.00;
            if (data.getOrderItems().size() > 0) {
                for (int i = 0; i < data.getOrderItems().size(); i++) {
                    servicePrice += data.getOrderItems().get(i).getServicePrice();
                }
            }

            // 实付款=商品总额+运费+安装费-优惠券金额
            detailActualPayment = data.getTotal() + data.getFreight() + servicePrice - discountPrice;
            String totalPrice = "共" + count + "件商品  合计:  ￥" + String.format("%.2f", detailActualPayment) + "  (含运费:  " + String.format("%.2f", data.getFreight()) + ")";
            tvTotalPrice.setText(totalPrice);


            tvBottomOrderRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (getOrderStatusType(data)) {
                        case WAITPAY:
                            Intent ti = new Intent(context, OrderPaidActivity.class);
                            ti.putExtra("storeId", data.getStoreId());
                            ti.putExtra("totalPrice", detailActualPayment);
                            ti.putExtra("orderNumber", data.getOrderNumber());
                            ti.putExtra("orderType", data.getOrderSubType());
                            ti.putExtra("name", data.getShippingAddressName());
                            ti.putExtra("contact", data.getShippingAddressContact());
                            ti.putExtra("address", data.getShippingAddress());
                            context.startActivity(ti);
                            break;
                        case DELIVERY:

                            String msg = "是否确定收货?";
                            for (int i = 0; i < data.getOrderItems().size(); i++) {
                                if (data.getOrderItems().get(i).getIsRefund().equals("true") && !data.getOrderItems().get(i).getRefundsStatus().equals("complete")) {
                                    msg = "该订单存在退款记录的宝贝,确认收货将关闭退款。";
                                    break;
                                }
                            }


                            new UniversalDialog(context).builder().setTitle("温馨提示")
                                    .setMsg(msg)
                                    .setPositiveButton("确定", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            updateOrderStatus("receipt", data.getUserOrderId() + "", getDataPosition());
                                        }
                                    }).setNegativeButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                }
                            }).show();


                            break;
                        case TOINSTALL:
                            String msg2 = "是否确定安装?";
                            for (int i = 0; i < data.getOrderItems().size(); i++) {
                                if (data.getOrderItems().get(i).getIsRefund().equals("true") && !data.getOrderItems().get(i).getRefundsStatus().equals("complete")) {
                                    msg2 = "该订单存在退款记录的宝贝,确认安装将关闭退款。";
                                    break;
                                }
                            }

                            new UniversalDialog(context).builder().setTitle("温馨提示")
                                    .setMsg(msg2)
                                    .setPositiveButton("确定", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            updateOrderStatus("install", data.getUserOrderId() + "", getDataPosition());
                                        }
                                    }).setNegativeButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                }
                            }).show();

                            break;
                    }
                }
            });
            tvBottomOrderCenter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (getOrderStatusType(data)) {
                        case WAITPAY:
                            new UniversalDialog(context).builder().setTitle("取消订单")
                                    .setMsg("确定取消该订单吗?取消后不可恢复")
                                    .setPositiveButton("确定", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            updateOrderStatus("cancel", data.getUserOrderId() + "", getDataPosition());


                                        }
                                    }).setNegativeButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                }
                            }).show();

                            break;
                        case WAITDELIVERY:
                            pushGoods(context, data.getStoreId(), data.getUserOrderId(), data.getFromGoodsId(), (data.getOrderItems() != null && data.getOrderItems().size() > 0) ? Integer.parseInt(data.getOrderItems().get(0).getItemTypeId()) : data.getFromGoodsId(),data.getOrderItems().get(0).getPrice()+"",data.getOrderItems().get(0).getLogo());
                            break;
                        case DELIVERY:
                            StringBuffer sbName = new StringBuffer();
                            StringBuffer sbNumber = new StringBuffer();
                            StringBuffer sbGoodIds = new StringBuffer();
                            if (data.getOrderItems().size() > 0) {
                                for (int i = 0; i < data.getOrderItems().size(); i++) {
                                    sbName.append(data.getOrderItems().get(i).getLogisticsCompany() + ",");
                                    sbNumber.append(data.getOrderItems().get(i).getLogisticsNumber() + ",");
                                    sbGoodIds.append(data.getOrderItems().get(i).getOrderItemId() + ",");

                                }
                            }
                            Intent tiLogistics1 = new Intent(context, LogisticsActivity.class);
//                            tiLogistics1.putExtra("name", sbName.toString());
//                            tiLogistics1.putExtra("number", sbNumber.toString());
//                            tiLogistics1.putExtra("goodsIds", sbGoodIds.toString());
                            tiLogistics1.putExtra("userOrderId", data.getUserOrderId());
                            context.startActivity(tiLogistics1);
                            break;
                        case TOINSTALL:
                            StringBuffer sbName2 = new StringBuffer();
                            StringBuffer sbNumber2 = new StringBuffer();
                            StringBuffer sbGoodIds2 = new StringBuffer();
                            if (data.getOrderItems().size() > 0) {
                                for (int i = 0; i < data.getOrderItems().size(); i++) {
                                    sbName2.append(data.getOrderItems().get(i).getLogisticsCompany() + ",");
                                    sbNumber2.append(data.getOrderItems().get(i).getLogisticsNumber() + ",");
                                    sbGoodIds2.append(data.getOrderItems().get(i).getOrderItemId() + ",");
                                }
                            }
                            Intent tiLogistics2 = new Intent(context, LogisticsActivity.class);
                            tiLogistics2.putExtra("userOrderId", data.getUserOrderId());
//                            tiLogistics2.putExtra("name", sbName2.toString());
//                            tiLogistics2.putExtra("number", sbNumber2.toString());
//                            tiLogistics2.putExtra("goodsIds", sbGoodIds2.toString());
                            context.startActivity(tiLogistics2);
                            break;
                        case COMPLETED:

                            String msg3 = "";
                            for (int i = 0; i < data.getOrderItems().size(); i++) {
                                if (data.getOrderItems().get(i).getIsRefund().equals("true") && !data.getOrderItems().get(i).getRefundsStatus().equals("complete")) {
                                    msg3 = "该订单存在退款记录的宝贝,确认评价将关闭退款。";
                                    break;
                                }
                            }
                            if (TextUtils.isEmpty(msg3)) {
                                Intent ti = new Intent(context, EvaluateActivity.class);
                                ti.putExtra("isFromDetail", false);
                                ti.putExtra("orderItemsList", (Serializable) data.getOrderItems());
                                ti.putExtra("userOrderId", data.getUserOrderId());
                                context.startActivity(ti);
                            } else {
                                new UniversalDialog(context).builder().setTitle("温馨提示")
                                        .setMsg(msg3)
                                        .setPositiveButton("确定", new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent ti = new Intent(context, EvaluateActivity.class);
                                                ti.putExtra("isFromDetail", false);
                                                ti.putExtra("orderItemsList", (Serializable) data.getOrderItems());
                                                ti.putExtra("userOrderId", data.getUserOrderId());
                                                context.startActivity(ti);
                                            }
                                        }).setNegativeButton("取消", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                    }
                                }).show();
                            }

                            break;
                        case COMMENTED:
                            Intent ti2 = new Intent(context, SeeEvaluateActivity.class);
                            ti2.putExtra("userOrderId", data.getUserOrderId());
                            context.startActivity(ti2);
                            break;
                        case CANCELLED:
                            new UniversalDialog(context).builder().setTitle("删除订单")
                                    .setMsg("确定删除该订单吗?删除后不可恢复")
                                    .setPositiveButton("确定", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            updateOrderStatus("del", data.getUserOrderId() + "", getDataPosition());


                                        }
                                    }).setNegativeButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                }
                            }).show();

                            break;

                    }

                }
            });
            tvBottomOrderLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (getOrderStatusType(data)) {
                        case WAITPAY:
                            pushGoods(context, data.getStoreId(), data.getUserOrderId(), data.getFromGoodsId(), (data.getOrderItems() != null && data.getOrderItems().size() > 0) ? Integer.parseInt(data.getOrderItems().get(0).getItemTypeId()) : data.getFromGoodsId(),data.getOrderItems().get(0).getPrice()+"",data.getOrderItems().get(0).getLogo());
                            break;

                    }

                }
            });

        }
    }

    //type 更改订单状态类型 cancel:撤销订单，receipt：确认收货，install：确认安装，del:删除订单
    private void updateOrderStatus(final String type, String orderId, final int position) {
        UpdateOrderStatusApi updateOrderStatusApi = new UpdateOrderStatusApi(new HttpOnNextListener<Double>() {
            @Override
            public void onNext(Double data) {
                //{0:不需要安装，1:需要安装}
                if (type.equals("del")) {
                    mObjects.remove(position);
                    notifyDataSetChanged();
                } else if (type.equals("cancel")) {
                    mObjects.remove(position);
                    notifyDataSetChanged();
                    Intent intent = new Intent();
                    intent.setAction(OrderActivity.orderAction);
                    intent.putExtra("currentId", 0);
                    context.sendBroadcast(intent);
                } else if (type.equals("receipt")) {
                    mObjects.remove(position);
                    notifyDataSetChanged();
                    Intent intent = new Intent();
                    intent.setAction(OrderActivity.orderAction);
                    if (data == 1) {
                        intent.putExtra("currentId", 3);
                    } else {
                        intent.putExtra("currentId", 4);
                    }
                    context.sendBroadcast(intent);

                } else if (type.equals("install")) {
                    mObjects.remove(position);
                    notifyDataSetChanged();
                    Intent intent = new Intent();
                    intent.setAction(OrderActivity.orderAction);
                    intent.putExtra("currentId", 4);
                    context.sendBroadcast(intent);
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(context, e.getMessage());
            }
        }, (RxAppCompatActivity) (context));
        updateOrderStatusApi.setSession(UserHelper.getInstance().getSessionInfoModel(context).getSession());
        updateOrderStatusApi.setType(type);
        updateOrderStatusApi.setOrderId(orderId);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(updateOrderStatusApi);
    }


    //waitPay：待付款，waitDelivery：待发货，delivery：已发货，toInstall：待安装，completed：已完成，commented：已评价，cancelled：已取消
    private int getOrderStatusType(OrdersListModel.ListBean data) {
        if (data.getOrderStatus().equals("waitPay")) {
            return WAITPAY;
        } else if (data.getOrderStatus().equals("waitDelivery")) {
            return WAITDELIVERY;
        } else if (data.getOrderStatus().equals("delivery")) {
            return DELIVERY;
        } else if (data.getOrderStatus().equals("toInstall")) {
            return TOINSTALL;
        } else if (data.getOrderStatus().equals("completed")) {
            return COMPLETED;
        } else if (data.getOrderStatus().equals("commented")) {
            return COMMENTED;
        } else if (data.getOrderStatus().equals("cancelled")) {
            return CANCELLED;
        } else {
            return DEFAULT;
        }
    }


    public static void getCustomer(final Context mContext, final int storeId, final int userOrderId, final int fromGoodsId) {
        final MProgressDialog mLoadView;
        mLoadView = new MProgressDialog.Builder(mContext)
                .isCanceledOnTouchOutside(true)
                .setOnDialogDismissListener(new MProgressDialog.OnDialogDismissListener() {
                    @Override
                    public void dismiss() {

                    }
                })
                .build();
        mLoadView.ShowLoadView();
        GetCustomerApi getCustomerApi = new GetCustomerApi(new HttpOnNextListener<MessageContacts>() {
            @Override
            public void onNext(MessageContacts messageContacts) {
                mLoadView.CancleLoadView();
                if (!TextUtils.isEmpty(messageContacts.getImKey())) {
                    EaseUser easeUser = new EaseUser(messageContacts.getImKey());
                    MyLog.e("yang", "storeId===" + storeId);
                    MyLog.e("yang", "fromGoodsId===" + fromGoodsId);
                    MyLog.e("yang", "messageContacts.getName()==" + messageContacts.getName() + "\nmessageContacts.getEmployeesName()==" + messageContacts.getEmployeesName());
                    if (storeId != 0) {
                        if (fromGoodsId != 0) {
                            easeUser.setNick(messageContacts.getName());
                            easeUser.setPhone(messageContacts.getPhone());
                            easeUser.setUserType(USER_TYPE_SYSTEM);
                        } else {
                            easeUser.setNick(messageContacts.getEmployeesName());
                            easeUser.setPhone(messageContacts.getContact());
                        }
                    } else {
                        easeUser.setNick(messageContacts.getName());
                        easeUser.setPhone(messageContacts.getPhone());
                        easeUser.setUserType(USER_TYPE_SYSTEM);
                    }
                    //  easeUser.setNick(storeId == 0 ? messageContacts.getName() : messageContacts.getEmployeesName());
                    easeUser.setAvatar(messageContacts.getUserLogo());
                    easeUser.setEmployeeId("" + messageContacts.getStoreEmployeesId());
                    //  easeUser.setPhone(storeId == 0 ? messageContacts.getPhone() : messageContacts.getContact());

//                    if(storeId == 0){
//                        easeUser.setUserType(USER_TYPE_SYSTEM);
//                    }
                    MessageHelper.getInstance().saveContact(easeUser);

                    Intent intent = new Intent(mContext, ChatActivity.class);
                    intent.putExtra(EaseConstant.EXTRA_USER_ID, messageContacts.getImKey());
                    mContext.startActivity(intent);
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
//                PhoneUtils.ShowToastMessage(mContext, e.getMessage());
                mLoadView.CancleLoadView();
            }
        }, (RxAppCompatActivity) mContext);
        getCustomerApi.setSession(UserHelper.getInstance().getSessionInfoModel(mContext).getSession());
        getCustomerApi.setStoreId(storeId + "");
        getCustomerApi.setType("order");
        getCustomerApi.setUserOrderId(userOrderId + "");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(getCustomerApi);
    }

//    //推送客服
//    public static void pushGoods(final Context mContext) {
//        PushGoodsApi pushGoodsApi = new PushGoodsApi(new HttpOnNextListener<Object>() {
//            @Override
//            public void onNext(Object s) {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                super.onError(e);
//                e.printStackTrace();
//            }
//        }, (RxAppCompatActivity) mContext);
//        pushGoodsApi.setSession(UserHelper.getInstance().getSessionInfoModel(mContext).getSession());
//        pushGoodsApi.setGoodsId("0");
//        HttpManager manager = HttpManager.getInstance();
//        manager.doHttpDeal(pushGoodsApi);
//    }

    //推送客服
    public static void pushGoodsData(final Context mContext, final int storeId, final int userOrderId, final int fromGoodsId, String price, String goodsLogo) {
        final MProgressDialog mLoadView;
        mLoadView = new MProgressDialog.Builder(mContext)
                .isCanceledOnTouchOutside(true)
                .setOnDialogDismissListener(new MProgressDialog.OnDialogDismissListener() {
                    @Override
                    public void dismiss() {

                    }
                })
                .build();
        mLoadView.ShowLoadView();
        String url = Constants.APP_API_BASE + Constants.OWN_PUSHGOODS;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(mContext).getSession());
        params.addBodyParameter("goodsId", fromGoodsId + "");
        if (!StringUtils.isEmpty(price)) {
            params.addBodyParameter("minPrice", price);
        }
        if (!StringUtils.isEmpty(goodsLogo)) {
            params.addBodyParameter("goodsLogo", goodsLogo);
        }
        MyLog.e("hep", fromGoodsId + "");
        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                //这个方法不提示错误信息。复制注意
                String data = PhoneUtils.checkResponseFlagLogin((Activity) mContext, result);
                boolean flag = JsonUtil.getJsonValuesBoolean(result, "flag");
                int errorCode = JsonUtil.getJsonValuesInt(result, "errorCode");
                MyLog.e("hep", result);
                try {
                    if (flag) {
//
                        PushGoodsModel pushGoodsModel = GsonUtils.fromJson(data, PushGoodsModel.class);

                        if (pushGoodsModel != null && !TextUtils.isEmpty(pushGoodsModel.getImKey())) {
                            if (pushGoodsModel.getImKey().contains("adm")) {
                                Intent intent = new Intent(mContext, ChatActivity.class);
                                intent.putExtra(EaseConstant.EXTRA_USER_ID, pushGoodsModel.getImKey());
                                mContext.startActivity(intent);
//                                mLoadView.CancleLoadView();
                            } else {
                                getCustomer(mContext, storeId, userOrderId, fromGoodsId);
                            }
                        }
                    } else if (errorCode != 110) {
                        PhoneUtils.ShowToastMessage(mContext, mContext.getResources().getString(R.string.customer_service_toast));
//
                    }
                } catch (Exception e) {
                    Log.e("King", "解析失败!");
                    e.printStackTrace();
//                    mLoadView.CancleLoadView();
                }
//                mLoadView.CancleLoadView();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
//                mLoadView.CancleLoadView();
            }

            @Override
            public void onCancelled(CancelledException cex) {
//                mLoadView.CancleLoadView();
            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });

    }

    //推送判断是门店还是系统客服
    public static void pushGoods(final Context mContext, final int storeId, final int userOrderId, final int fromGoodsId, final int goodsId, String price, String goodsLogo) {
        if (storeId != 0) {
            if (fromGoodsId != 0) {
                pushGoodsData(mContext, storeId, userOrderId, goodsId,price,goodsLogo);
            } else {
                getCustomer(mContext, storeId, userOrderId, fromGoodsId);
            }
        } else {
            pushGoodsData(mContext, storeId, userOrderId, goodsId,price,goodsLogo);

        }

    }
}
